import moment, { Moment } from "moment";

import { isArray } from "@/utils/is";

/**
 * @description 格式化表格单元格默认值(el-table-column)
 * @param {Number} row 行
 * @param {Number} col 列
 * @param {String} callValue 当前单元格值
 * @return string
 * */
export function defaultFormat(row: number, col: number, callValue: any) {
	// 如果当前值为数组,使用 / 拼接（根据需求自定义）
	if (isArray(callValue)) return callValue.length ? callValue.join(" / ") : "--";
	return callValue ?? "--";
}

/**
 * @description 处理无数据情况
 * @param {String} callValue 需要处理的值
 * @return string
 * */
export function formatValue(callValue: any) {
	// 如果当前值为数组,使用 / 拼接（根据需求自定义）
	if (isArray(callValue)) return callValue.length ? callValue.join(" / ") : "--";
	return callValue ?? "--";
}

export interface DataFormat {
	date: "date";
}
export type DateFormatType = "date" | "datetime";
/**
 * 数据格式化
 * @param target 格式化处理器
 * @param value 需要格式的内容
 * @returns tesult
 * @example console.log(dataFormat("datetime", "2023-11-15"))
 */
export function dataFormat(target: DateFormatType, value: any) {
	let proxy = proxyFormat;
	let call = proxy[target];
	if (!call) {
		return "-";
	}
	return call(value);
}

const proxyFormat: { [key: string]: any } = {
	date: (val: string) => {
		let dts = moment(val);
		if (!dts.isValid()) {
			return "-";
		}
		return dts.format("YYYY-MM-DD");
	},
	datetime: (val: string) => {
		let dts = moment(val);
		if (!dts.isValid()) {
			return "-";
		}
		return dts.format("YYYY-MM-DD HH:mm:ss");
	}
};

export function duration(time: Moment) {
	if (!time.isValid()) {
		return "0秒";
	}
	let start = time.valueOf();
	let end = moment().valueOf();
	let diff = end - start;
	return convertTime(diff);
}
export function convertTime(time: number) {
	time = time / 1000;
	let res: string = "";
	let unit;
	if (time / 60 < 1) {
		res = time.toFixed(1);
		unit = "秒";
	} else if (time / 60 / 60 < 1) {
		res = (time / 60).toFixed(1);
		unit = "分钟";
	} else if (time / 60 / 60 / 24 < 1) {
		res = (time / 60 / 60).toFixed(1);
		unit = "小时";
	} else if (time / 60 / 60 / 24 / 30 < 1) {
		res = (time / 60 / 60 / 24).toFixed(1);
		unit = "天";
	} else if (time / 60 / 60 / 24 / 30 / 12 < 1) {
		res = (time / 60 / 60 / 24 / 30).toFixed(1);
		unit = "月";
	} else {
		res = "0.0";
		unit = "秒";
	}
	// if (time / 60 < 1) {
	//   time = time.toFixed(1)
	//   unit = '秒'
	// } else {
	//   time = (time / 60).toFixed(2)
	//   unit = '分钟'
	// }
	let data = res.split(".");
	let suffix = Number(data[1]);
	res = data[0] + (suffix != 0 ? "." + suffix : "") + unit;
	return res;
}

/**
 * 流量格式
 * @param size
 * @returns {{size: any, unit: *}}
 */
export function formatSize(size: number) {
	let res: string = "";
	let unit;
	if (size / 1024 < 1) {
		res = size.toFixed(1);
		unit = "B";
	} else if (size / 1024 / 1024 < 1) {
		res = (size / 1024).toFixed(1);
		unit = "KB";
	} else if (size / 1024 / 1024 / 1024 < 1) {
		res = (size / 1024 / 1024).toFixed(1);
		unit = "MB";
	} else if (size / 1024 / 1024 / 1024 / 1024 < 1) {
		res = (size / 1024 / 1024 / 1024).toFixed(1);
		unit = "GB";
	}
	size = size % 1.0 == 0 ? parseInt(res) : size;
	return { size: size, unit: unit };
}
